package com.pxmeta.party.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.common.utils.agora.AgoraClient;
import com.pxmeta.party.entity.Gift;
import com.pxmeta.party.entity.PartyGiveGift;
import com.pxmeta.party.entity.vo.ChannelUserVO;
import com.pxmeta.party.entity.vo.GiftItemsVO;
import com.pxmeta.party.entity.vo.PartyCharmValuesVO;
import com.pxmeta.party.mapper.GiftMapper;
import com.pxmeta.party.mapper.PartyGiveGiftMapper;
import com.pxmeta.party.service.IPartyGiveGiftService;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.service.IUserService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PartyGiveGiftServiceImpl extends ServiceImpl<PartyGiveGiftMapper, PartyGiveGift> implements IPartyGiveGiftService {

    @Autowired
    private GiftMapper giftMapper;

    @Autowired
    private AgoraClient agoraClient;

    @Autowired
    private IUserService userService;

    @Override
    public void saveRecord(Long partyId, Long userId, Long targetUserId, Integer giftId, Integer giftCount, Integer giftPrice) {

        Gift gift = giftMapper.selectById(giftId);
        PartyGiveGift partyGiveGift = new PartyGiveGift();
        partyGiveGift.setPartyId(partyId.toString());
        partyGiveGift.setUserId(userId);
        partyGiveGift.setTargetUserId(targetUserId);
        partyGiveGift.setGiftId(giftId);
        partyGiveGift.setGiftIcon(gift.getIcon());
        partyGiveGift.setGiftTotalPrice(giftPrice);
        partyGiveGift.setGiftCount(giftCount);

        this.save(partyGiveGift);
    }

    @Override
    public List<GiftItemsVO> getGiveItems(String partyId, String username) {

        User user = userService.getByUsername(username);

        List<GiftItemsVO> list = baseMapper.getGiveItems(partyId, user.getId());
        return list;
    }

    @Override
    public List<GiftItemsVO> getPartyGiveGiftList() {

        return baseMapper.getPartyGiveGiftList();
    }

    @Override
    public IPage<GiftItemsVO> getPartyGiveGiftPage(Integer page, Integer size) {
        return baseMapper.getPartyGiveGiftPage(new Page<>(page,size));
    }

    @Override
    public List<PartyCharmValuesVO> getPartyCharmValues(String partyId,List<String> usernames) {


        if (CollectionUtil.isEmpty(usernames)){
            ChannelUserVO vo = agoraClient.getUsers(partyId).toJavaObject(ChannelUserVO.class);
            List<Integer> broadcasters = vo.getBroadcasters();
            if (CollectionUtil.isNotEmpty(broadcasters) ){
                usernames = broadcasters.stream().map(String::valueOf).collect(Collectors.toList());
            }
        }

        if (usernames.isEmpty()){
            return Lists.newArrayList();
        }
        List<User> users = userService.listByUsernames(usernames);
        List<Long> userIds = users.stream().map(User::getId).collect(Collectors.toList());

        return baseMapper.getPartyCharmValues(partyId,userIds);
    }
}
